package com.wang.controller.api;

import com.wang.entity.User;
import com.wang.service.CartService;
import com.wang.utils.ConstantUtil;
import com.wang.utils.ResponseEnum;
import com.wang.utils.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @Classname ApiCartController
 * @Description TODO
 * @Date 2024/10/14 8:43
 * @Created by pc
 */
@RestController
@Slf4j
@RequestMapping("/api/cart")
@Api(value = "购物车",tags = "ApiCartController")
public class ApiCartController {

    @Resource
    private CartService cartService;

    @GetMapping("/findCartCount")
    @ApiOperation(value = "获取用户购物车数量",notes = "前台根据用户id获取该用户购物车数量")
    public ResponseUtil findCountByUserId(HttpSession session){
        User loginUser = (User) session.getAttribute(ConstantUtil.LOGIN_USER);
        ResponseUtil r= null;
        if(loginUser!=null){
            r = cartService.findCountByUserId(loginUser.getId());
        }else{
            r = ResponseUtil.get(ResponseEnum.CART_FIND_COUNT_FAILURE);
        }
        return r;
    }

    @GetMapping("/findCart")
    @ApiOperation(value = "获取用户购物车详情",notes = "前台根据用户id获取该用户购物车详情")
    public ResponseUtil findCartByUserId(HttpSession session){
        User loginUser = (User) session.getAttribute(ConstantUtil.LOGIN_USER);
        return cartService.findCartByUserId(loginUser.getId());
    }

    @PostMapping("/updateCart")
    // id  数量 选中状态
    @ApiOperation(value = "更新购物车",notes = "前台更新购物车数量/状态")
    public ResponseUtil updateCart(
            @RequestParam("id")Integer id,
            @RequestParam("quantity")Integer quantity,
            @RequestParam("checked")Integer checked
    ){
        return cartService.updateCart(id,quantity,checked);
    }

    // 删除单个商品 根据id删除
    @PostMapping("/delCartById")
    @ApiOperation(value = "删除购物车",notes = "前台根据id删除购物车数量")
    public ResponseUtil delCartById(@RequestParam("id")Integer id){
        return cartService.delCartById(id);
    }

    // 删除单个商品 根据id删除
    @PostMapping("/delCartByIds")
    @ApiOperation(value = "批量删除购物车",notes = "前台根据id批量删除购物车数量")
    public ResponseUtil delCartByIds(@RequestParam("idArr")Integer[] idArr){
        return cartService.delCartByIds(idArr);
    }

    // 清空购物车 根据登录用户的id删除
    @PostMapping("/delCartByUserId")
    @ApiOperation(value = "清空购物车",notes = "前台根据用户id清空删除购物车数量")
    public ResponseUtil delCartByUserId(HttpSession session){
        User loginUser = (User) session.getAttribute(ConstantUtil.LOGIN_USER);
        return cartService.delCartByUserId(loginUser.getId());
    }


}
